﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace AcesHigh.GUI
{

    internal delegate void CardClickedEvent(TableDeckPanel panel, PictureCard card);

    internal class TableDeckPanel : DeckPanel
    {
        public event CardClickedEvent CardClicked;

       // List<Card> cards;
      //  List<PictureBox> cardImages;
        

        new protected const int Y_OFFSET = 25;

        public TableDeckPanel()
        {
            AddDragHandlers();
        }

        
        
        private void AddDragHandlers()
        {
            this.DragEnter += new DragEventHandler(Drag_DragEnter);
            this.DragDrop += new DragEventHandler(Drag_DragDrop);
        }


        internal override void addPictureCardEvents()
        {
            this.addPictureCardEvents();
            picCards[picCards.Count - 1].MouseDown += new MouseEventHandler(CardPanel_MouseDown);
        }

        internal override void removePictureCardEvents()
        {
            this.addPictureCardEvents();
            (picCards[picCards.Count - 2]).MouseDown -= CardPanel_MouseDown;
        }

        public void CardPanel_MouseDown(Object obj, MouseEventArgs args)
        {
            if (args.Clicks == 1)
            {
                PictureBox pictureBox1 = (PictureBox)obj;
                DoDragDrop(this, DragDropEffects.Move);
            }
            else
            {
                CardDoubleClick(obj, args);
            }
        }

        public void Drag_DragEnter(object obj, DragEventArgs args)
        {
            Console.WriteLine("Drag enter");
         //   if (this == obj) return;
            if (picCards.Count == 0)
            {
                if(args.Data.GetDataPresent(typeof(TableDeckPanel)))
                {
                   
                    args.Effect = DragDropEffects.Move;
                } else {
                    args.Effect = DragDropEffects.None;
                }
            }
        }

        public void Drag_DragDrop(object obj, DragEventArgs args)
        {
            Console.WriteLine("Drag enter");
         //   if (this == obj) return;
            Console.WriteLine(picCards.Count);
            if (picCards.Count == 0)
            {
                TableDeckPanel cp = (TableDeckPanel)args.Data.GetData(typeof(TableDeckPanel));
 
                this.addPictureCard(cp.PopPictureCard());
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (picCards != null) picCards = null;
              //  if (cardImages != null) cardImages = null;
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // CardPanel
            // 
            this.AutoSize = true;
            this.Size = new System.Drawing.Size(72, 96);
            this.ResumeLayout(false);

        }

        
    }
}
